import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class DKSegmentSwitchWidget extends StatefulWidget {
  final List<String> titles;
  final int  selectIndex;
  final TextStyle  selectTextStyle;
  final TextStyle  unSelectTextStyle;
  final Function(int index)  selectCallback;
  final Color selectLineColor;

  DKSegmentSwitchWidget({ @required this.titles,this.selectIndex = 0,this.selectTextStyle = const TextStyle(color: Color(0xff3DBCC6),fontSize: 14),
  this.unSelectTextStyle = const TextStyle(color: Color(0xff333333),fontSize: 14),this.selectLineColor = const Color(0xff3DBCC6),this.selectCallback});

  @override
  State<StatefulWidget> createState() {
    return SectionSelectState(this.titles,this.selectIndex,this.selectTextStyle,this.unSelectTextStyle,this.selectLineColor,this.selectCallback);
  }
}

class SectionSelectState extends State<DKSegmentSwitchWidget> {

   List<String> titles;
   int  selectIndex;
   TextStyle  selectTextStyle;
   TextStyle  unSelectTextStyle;
   Function(int index)  selectCallback;
   Color selectLineColor;

  SectionSelectState(this.titles,this.selectIndex,this.selectTextStyle,this.unSelectTextStyle,this.selectLineColor,this.selectCallback);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 44,
      width: double.infinity,
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: getTitleHeadWidgets(),
      ),
    );
  }

   List<Widget> getTitleHeadWidgets() {
     List<Widget> widgets = List<Widget>();
     for (int i = 0; i < titles.length; i++) {
       String str = titles[i];
       widgets.add(Expanded(
         child: GestureDetector(
           onTap: () {
             setState(() {
               selectIndex = i;
             });
             if(selectCallback!= null) {
               selectCallback(selectIndex);
             }
           },
           child: Container(
             height: 44,
             child: Column(
               mainAxisAlignment: MainAxisAlignment.start,
               children: <Widget>[
                 Expanded(
                   child:Center(
                     child: Text(str,style: (selectIndex == i)? selectTextStyle : unSelectTextStyle,),
                   ),
                 ),
                 Container(
                   height: 2,
                   width: 45,
                   color:  (selectIndex == i)? selectLineColor : Colors.white,
                 ),
               ],
             ),
           ),
         ),
       ));
     }
     return widgets;
   }
}
